using System;
using System.Configuration;
using System.Linq;
using System.ServiceModel.Configuration;

namespace Kiwi.Container.Wcf
{
    public class DependencyInjectionBehaviorExtensionElement : BehaviorExtensionElement
    {
        private const string InjectModeAttributeName = "injectMode";

        public override Type BehaviorType
        {
            get { return typeof(DependencyInjectionServiceBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new DependencyInjectionServiceBehavior(DependencyInjection.Properties.Parse(InjectMode));
        }

        public string InjectMode
        {
            get
            {
                return (string)base["injectMode"];
            }
        }

        protected override ConfigurationPropertyCollection Properties
        {
            get
            {
                var properties = base.Properties;
                if (!properties.Cast<ConfigurationProperty>().Any(c => c.Name == InjectModeAttributeName))
                {
                    properties.Add(new ConfigurationProperty(InjectModeAttributeName, typeof (string), ""));
                }
                return properties;
            }
        }
    }
}